package com.sc;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 获取泛型
 */
public class GenericType {
}
class Person {
    private String nameString = "233";

    public String getNameString() {
        return nameString;
    }

    public void setNameString(String nameString) {
        this.nameString = nameString;
    }

    @Override
    public String toString() {
        return "Person [nameString=" + nameString + "]";
    }

}

//基本dao
 abstract class BaseDao<T> {
    private Class<T> clazz = null;
    {
        Type type = getClass().getGenericSuperclass();
        if( type instanceof ParameterizedType ){
            ParameterizedType pType = (ParameterizedType)type;
            Type claz = pType.getActualTypeArguments()[0];
            if( claz instanceof Class ){
                this.clazz = (Class<T>) claz;
            }
        }
    }

    public T getEntity() throws InstantiationException, IllegalAccessException{
        return this.clazz.newInstance();
    }
}

//实现
 class PersonDao extends BaseDao<Person> {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        PersonDao pDao = new PersonDao();
        System.out.println( pDao.getEntity().toString() );
    }
}
